package com.heima.admin.job;

import com.heima.admin.service.NewsAutoScanService;
import com.heima.apis.wemedia.IWeMediaClient;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Ronaldo
 * @date 2021/8/12
 */
@Component
@Slf4j
public class WeMediaNewsAutoScanJob {

    @Autowired
    private IWeMediaClient weMediaClient;

    @Autowired
    private NewsAutoScanService newsAutoScanService;

    /**
     * 每天0点执行一次
     * @param param
     * @return
     * @throws Exception
     */
    @XxlJob("weMediaAutoScanJob")
    public ReturnT<String> autoScanJob(String param) throws Exception {

        List<Integer> idList = weMediaClient.findReleaseNewsId();

        if(idList!=null && idList.size()>0){
            log.info("开始执行自媒体文章自动发布，此时时间戳：{}" , System.currentTimeMillis()/1000);
            for (Integer wmNewsId : idList) {
                log.info("自动发布文章：{}", wmNewsId);
                newsAutoScanService.newsAutoScanById(wmNewsId);
            }
            log.info("完成执行自媒体文章自动发布，此时时间戳：{}" ,  System.currentTimeMillis()/1000);
        } else {
            log.info("暂无可自动发布的文章,时间：{} ",  System.currentTimeMillis()/1000);
        }

        return ReturnT.SUCCESS;
    }

}